/*
 * @Author: Creator
 * @Date: 2020-10-09 14:22:51
 * @LastEditors: Creator
 * @LastEditTime: 2020-10-09 21:47:03
 * @Description: file content
 */

#include "medicalrecord.h"
#include "global.h"
extern PatientNode *PatientList;
extern DoctorNode *DoctorList;
extern MedRecNode *MedRecList;

MedRecNode* createNullMedRecNode()
{
    MedRecNode *temp = (MedRecNode*)malloc(sizeof(MedRecNode));
    temp->next = temp->pre = NULL;
    return temp;
}

MedRecNode* readAMedRec(FILE *fp)
{
    MedRecNode *temp = createNullMedRecNode();
    temp->data.Time = readTime(fp);
    temp->data.patient = findPatientNode(PatientList,freadID(fp));
    temp->data.doctor = find_Doctor(DoctorList,freadID(fp));
    temp->data.dignosis.CheckCost = freadFloat(fp);
    strcmp(temp->data.dignosis.hospital.patientid,
            temp->data.patient->idetify_card);
    temp->data.dignosis.hospital.in = readTime(fp);
    temp->data.dignosis.hospital.preout = readTime(fp);
    temp->data.dignosis.hospital.deposit = freadFloat(fp);
    return temp;
}

MedRecNode* loadMedRecList()
{
    MedRecNode* medreclist;
    FILE* fp = NULL;
    if ((fp = fopen("medicalrecord.json", "r")) == NULL)
    {
        printf("ERROR:Can't open patient database!\n");
        return 0;
    }
    fscanf(fp, "%d", &MedicalRecordNum);
    MedRecNode* temp = NULL;
    medreclist = temp = readAMedRec(fp);
    int num = 1;                                                // 用于记录已经录入的数目
    while (!feof(fp) && num++ < MedicalRecordNum)
    {
        temp->next = readAMedRec(fp);
        temp->next->pre = temp;
        temp = temp->next;
    }
    temp->next = NULL;
    fclose(fp);
    return medreclist;
}
